var newsTable;
//获取试题选项信息
function getExamOptionsList(name) {
    if (typeof newsTable !== 'undefined') {
        newsTable.fnPageChange('first');
        return;
    }
    
    var i = 1;
    newsTable = $("#newsList").DataTable({
        "sAjaxSource": _path + "/examOptions/queryByPage",
        "fnServerParams": function (aoData) {
            aoData.push(
            	{ "name": "questionId", "value": $("#questionId").val()},	
                { "name": "optionName", "value": $("#optionName").val() },
                { "name": "fromDate", "value": $("#fromDate").val() },
                { "name": "toDate", "value": $("#toDate").val() }
            );
        },
        "sServerMethod": "POST",
        "sAjaxDataProp": "dataList",
        "fnCreatedRow": function () {
            i++;
        },
        "fnPreDrawCallback": function () {
            i = 1;
        },
        "aoColumns": [
            { "sTitle": "序号", "sWidth": "2%",
                "mData": function () {
                    return i;
                }
            },
            { "sTitle": "选项内容", "sWidth": "20%" ,"mData": 
	            	function(data){
	        		var temps = '<a href="' + _path + '/examOptions/edit/' + data.id + '">'+data.optionName+'</a>';
	        		return temps;
	        	}
	        },
	        { "sTitle": "显示顺序", "sWidth": "20%" ,"mData": 
	            	function(data){
	        		var temps ="<input type=\"text\" id=\"sort"+data.id+"\"  style=\"width:30px;\" value=\""+data.sort+"\" onblur=\"updateSort('"+data.id+"','"+name+"');\">"
	        		return temps;
        		}
	        },
            { "sTitle": "创建时间", "mData": "insertTime", "sWidth": "10%" },
            { "sTitle": "操作", "sWidth": "10%",
                "mData": function (data) {
                		var status = '<a href="' + _path + '/examOptions/edit/' + data.id + '" class="btn default btn-xs purple"><i class="fa fa-edit" title="编辑"></i></a>';
                        status += '&nbsp;<a href="javascript:inquiry('+data.id+','+data.questionId+')"  class="btn default btn-xs red"><i class="fa fa-trash-o" title="删除"></i></a>';
                        return status;
                }
            }
        ]
    });
}


function inquiry(id,questionId){
	layer.confirm('您确定要删除吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			layer.closeAll('dialog');
			$.ajax({
				url :  _path + "/examOptions/del?id=" + id + "&questionId="+questionId,
				type : "get",
				dataType : "json",
				async : false,
				error : function(request) {
					alert("连接错误！");
				},
				success : function(data) {
					  if(data.msg=='成功'){
						  layer.close
						  App.bubble(200, "删除成功");
						  getExamOptionsList();
					  }else{
						  App.bubble(200, "删除异常");
					  }
				}
			});
		}, function(){
		
		});
}



//修改显示顺序
function updateSort(id,name){
	var sort = document.getElementById("sort"+id).value;
	$.ajax({										
	    url: _path + "/examOptions/updateSort/?id="+id+"&sort="+sort,
	    type: "POST",
	    dataType: "json",
	    async: false,
	    success: function(jsonMap) {
	    	var result = jsonMap.result;
	    	getExamQuestionsList(name);
	    }
	});
}

//获取所有项目名称
//function queryAllProject(pId){
//	 $.ajax({										
//		    url: _path + "/examQuestions/queryAllProject",
//		    type: "POST",
//		    dataType: "json",
//		    async: false,
//		    success: function(jsonMap) {
//		    	var list = jsonMap.dataList;
//		    	$("#projectId").html("");
//		    	$("#projectId").append("<option selected value=''>请选择</option>");
//		        if(list.length>0){
//		        	for(var i=0;i<list.length;i++){
//						var projects = list[i];
//						var op ="";
//						  if ( pId == projects.id) {
//							  op = "<option value="+projects.id+" selected>"+projects.name+"</option>";
//						  }else{
//							  op = "<option value="+projects.id+" >"+projects.name+"</option>";
//						  }
//						$("#projectId").append(op);
//		        	}
//		        }
//		    }
//		});
//}